<?php
/*---------------------------------------------------------------------------
 小微OA系统 - 让工作更轻松快乐

 Copyright (c) 2013 http://www.smeoa.com All rights reserved.

 Author:  jinzhu.yin<smeoa@qq.com>

 Support: https://git.oschina.net/smeoa/xiaowei
 -------------------------------------------------------------------------*/

namespace Home\Model;
use Think\Model;

class  ProjectModel extends CommonModel {
	// 自动验证设置
	function get_member($project_id) {
		if (!empty($project_id)) {
			$where_member['project_id'] = array('eq', $project_id);
			$member_list = M('ProjectMember') -> where($where_member) -> getField('user_id', true);
 			
			if(!empty($member_list)){
				$where_user['id'] = array('in', $member_list);	
			}else{
				$where_user['_string']='1=2';
			}
			
			$member_list = M('User') -> where($where_user) -> getField('id,name');
		}
		
		return $member_list;
	}

	function set_member($member, $project_id) {
		$where_del['project_id'] = array('eq', $project_id);
		M("ProjectMember") -> where($where_del) -> delete();

		$member_list = array_filter(explode(';', $member));

		$data['project_id'] = $project_id;
		foreach ($member_list as $val) {
			$temp = explode('|', $val);
			$data['user_id'] = $temp[1];
			M("ProjectMember") -> add($data);
		}
	}

	function get_executor($task_id) {
		if (!empty($task_id)) {
			$where_executor['task_id'] = array('eq', $task_id);
			$executor_list = M('ProjectTaskExecutor') -> getField('user_id', true);
			if (!empty($executor_list)) {
				$where_user['id'] = array('in', $executor_list);
				$executor_list = M('User') -> where($where_user) -> getField('id,name');
			} else {
				return;
			}
		}
		return $executor_list;
	}

	function set_executor($executor, $task_id) {
		$where_del['task_id'] = array('eq', $task_id);
		M("ProjectTaskExecutor") -> where($where_del) -> delete();

		$$executor_list = array_filter(explode(';', $executor));
		$data['task_id'] = $task_id;
		foreach ($$executor_list as $val) {
			$temp = explode('|', $val);
			$data['user_id'] = $temp[1];
			M("ProjectTaskExecutor") -> add($data);
		}
	}

	function set_workload($project_id) {
		$where_all['project_id'] = array('eq', $project_id);
		$all = M('ProjectTask') -> where($where_all) -> sum('workload');

		$where_finish['project_id'] = array('eq', $project_id);
		$where_finish['is_finish'] = array('eq', 2);
		$finish = M('ProjectTask') -> where($where_finish) -> sum('workload');

		$data['all'] = $all;
		$data['yes'] = $finish;
		$data['id'] = $project_id;

		M('Project') -> save($data);
	}

	function calc_finish_rate($project_id) {
		$where_all['project_id'] = array('eq', $project_id);
		$where_all['is_del'] = array('eq', 0);
		$all = M('ProjectTask') -> where($where_all) -> sum('workload');

		$where_finish['project_id'] = array('eq', $project_id);
		$where_finish['is_finish'] = array('eq', 4);
		$where_finish['is_del'] = array('eq', 0);
		$finish = M('ProjectTask') -> where($where_finish) -> sum('workload');

		$data['finish_rate'] = round($finish / $all,2)*100;
		$data['id'] = $project_id;
		M('Project') -> save($data);
	}

}
?>